<?php

class PriorityDao extends Dao {

    function __construct() {
        parent::__construct();
        $this->table = 'priority';
    }	

    public function insertObject($obj){
            $result = DB::insert(DB::raw("INSERT INTO priority (name, level, description) ".
                    "VALUES ('".$obj->getName()."' ".
                            ", '".$obj->getLevel()."' ".
                            ", '".$obj->getDescription()."' ".
                            ")"));
            if (!$result) { return null; }

            return $obj->getId();
    }
    
    public function updateObject($obj, $old_id){
        DB::update(DB::raw("UPDATE priority SET name='".$obj->getName().
                           "', level='".$obj->getLevel().
                           "', description='".$obj->getDescription().
                           "' WHERE id=".$old_id." "));
        return true;
    
    }

    public function deleteObject($id){
        DB::delete(DB::raw("DELETE FROM priority where id='".$id."'"));
        
        return true;
    }
    
    public function getObject($id){
        $result = DB::select(DB::raw("SELECT * FROM priority where id='".$id."'"));
        
        if (!$result) { return null; }
        return $this->toObject($result[0]);
    }
    
    
    public function getList($criteria = NULL){
        $result = DB::select(DB::raw("SELECT * FROM priority"));
        $list = array();
        foreach ($result as $rowdata){
            $list[] = $this->toObject($rowdata);
        }
        return $list;
    }
    
    function updatePriorityId($priorityId,$oldPriorityId){
         try {
            DB::select( DB::raw("update priority set id = '$priorityId' where id = '$oldPriorityId' ") );
            return true;
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
        }
    }
    
     protected function toObject($rowdata=null){
        $obj = new Priority();
        $obj->setId($rowdata->id);
        $obj->setName($rowdata->name);
        $obj->setLevel($rowdata->level);
        $obj->setDescription($rowdata->description);
        $obj->setIsLoaded(true);
        
        return $obj;
    }
    
}
